﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Accord.MachineLearning;
using Accord.Imaging;
using Accord.Math;
using Accord.Statistics;

namespace KMeansClustering
{
    public partial class ResultsForm : Form
    {
        public ResultsForm(int result)
        {
            InitializeComponent();
            label1.Text = "result is: " + result;
            // Load original image-----------------------here the user will have to select one picture from the data base
            Bitmap image = Properties.Resources._1;

            // Transform the image into an array of pixel values
            double[][] pixels = image.ToDoubleArray();


            // Create a K-Means algorithm using given k and a
            //  square euclidean distance as distance metric.
            KMeans kmeans = new KMeans(result, Distance.SquareEuclidean);

            // Compute the K-Means algorithm until the difference in
            //  cluster centroids between two iterations is below 0.05
            int[] idx = kmeans.Compute(pixels, 0.05);  //tolerance value


            // Replace every pixel with its corresponding centroid
            pixels.ApplyInPlace((x, i) => kmeans.Clusters.Centroids[idx[i]]);


            pictureBox1.Image = pixels.ToBitmap(image.Width, image.Height);
        }
    }
}
